// pages/personal/keep/keep.js
const app = getApp();
Page({
    data: {
        loading: false,
        page: 1,
        total: 2,
        list: [],
    },
    onLoad: function (options) {
        this.loadData();
    },


    loadData: function () {
        if (this.data.loading || this.data.page > this.data.total) {
            return;
        }
        this.setData({
            loading: true
        });
        app.http.get("member/collect", {
            data: { page: this.data.page },
            showMsg: true
        }).then(result => {
            this.setData({
                recommend: result.data.recommend,
                list: this.data.list.concat(result.data.items),
                page: this.data.page + 1,
                total: result.data.total,
                loading: false,
            })
            wx.stopPullDownRefresh()
        })
    },
    onReachBottom: function () {
        this.loadData();
    },
    onPullDownRefresh() {
        this.setData({
            page: 1,
            total: 2,
            list: []
        })
        this.loadData()
    },
    view(evt) {
        wx.navigateTo({
            url: '/pages/goods/detail/detail?id=' + evt.currentTarget.dataset.id,
        })
    },

    //删除收藏
    removeKeep: function (evt) {
        var id = evt.currentTarget.dataset.id;
        wx.showModal({
            title: '提示',
            content: '是否移除该收藏？',
            success: res => {
                if (res.confirm) {
                    wx.showLoading({
                        title: '加载中...',
                    })
                    app.http.get("goods/collect", {
                        data: { id: id }
                    }).then(r => {
                        this.setData({
                            list: this.data.list.filter(function (o) {
                                return o.id != id;
                            }),
                        })
                        wx.hideLoading();
                    }).catch(err => {
                        this.toast.show({
                            title: "操作失败"
                        });
                    })
                }
            }
        })
    }
})